<template>
  <!-- 竣工项目归档台账 -->
  <base-breadcrumb class="archiveStandingBook-container">
    <searchData :searchData="searchArr" @search="handleSearch"></searchData>
    <a-card :bordered="false">
      <!-- <title-name title="竣工项目归档台账" /> -->
      <excel v-show="false" ref="exportBtn" :fileName="exportFileName" is-click-generate="false">
      </excel>
     <list-table
        ref="table"
        row-key="id"
        :columns="columns"
        :request="loadData"
        :alert="true"
      >
        <template slot="action" slot-scope="text, record">
          <div class="pub-table-list-action">
            <span @click="handleUpdate(record, 'look')">查看</span>
            <span @click="handleUpdate(record, 'export')"><a-spin :spinning="!!record.loading" />导出</span>
          </div>
        </template>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>

<script>
// import { STable } from '@/components' // 表格
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import { getArchiveStandingBook, getArchiveDocList } from '@/api/office/archiveManage/archiveDoc.js'
import { STATUS_MAP_Number } from '@/config/businessConfig.js'
import OrgTreeSelect from '@/components/OrgTreeSelect'

const searchArr = [
  // {
  //   name: '分公司',
  //   valueKey: 'branchOrgId',
  //   type: SelectSubCompany,
  // },
  {
    name: '所属单位',
    valueKey: 'searchQuery',
    type: OrgTreeSelect,
  },
  {
    name: '归档年度',
    value: 'archiveYear',
    type: 'year',
    placeholder: '请选择',
  },
]
const columns = [
  {
    title: '序号',
    width: 80,
    dataIndex: 'index',
    customRender() {
      return arguments[2] + 1
    },
  },
  {
    title: '分公司',
    dataIndex: 'branchOrgName',
  },
  {
    title: '归档年度',
    dataIndex: 'archiveYear',
  },
  {
    title: '计划归档项目数',
    dataIndex: 'planArchiveProjectNum',
  },
  {
    title: '已归档项目数',
    dataIndex: 'hasArchiveProjectNum',
  },
  {
    title: '超期未归档项目数',
    dataIndex: 'overNoArchiveProjectNum',
  },
  {
    title: '提交延期归档申请数',
    dataIndex: 'delayArchiveApplyNum',
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '180px',
    align: 'center',
    scopedSlots: { customRender: 'action' },
  },
]
export default {
  name:'a'+Date.now(),

  components: {
    // STable,
  },
  data() {
    return {
      searchArr,
      columns: columns,
      queryParam: {},
      exportFileName: '',
    }
  },
  methods: {
    async handleExport(data) {
      try {
        if(data.loading) return
        this.$set(data, 'loading', true)
        const { branchOrgId, branchOrgName, archiveYear } = data
        const params = {
          needCount: false,
          query: {
            branchOrgId: branchOrgId,
            archiveYear: archiveYear,
          },
        }
        const res = await getArchiveDocList(params)
        if (!res.data.length) {
          this.$message.warning('无导出数据')
        }
        const columns = [
          {
            title: '分公司',
            dataIndex: 'branchOrgName',
          },
          {
            title: '项目',
            dataIndex: 'projectName',
            ellipsis: true,
          },
          {
            title: '开工日期',
            dataIndex: 'startWorkingTime',
          },
          {
            title: '竣工日期',
            dataIndex: 'completionTime',
          },
          {
            title: '计划归档日期',
            dataIndex: 'archiveTime',
          },
          {
            title: '实际归档日期',
            dataIndex: 'actualArchiveTime',
            scopedSlots: { customRender: 'actualArchiveTime' },
          },
          {
            title: '是否已归档',
            dataIndex: 'ifCheckArchive',
            scopedSlots: { customRender: 'ifCheckArchive' },
          },
          {
            title: '是否已超期',
            dataIndex: 'ifOver',
            scopedSlots: { customRender: 'ifOver' },
          },
          {
            title: '是否申请延期归档',
            dataIndex: 'ifArchiveExtendedApply',
            scopedSlots: { customRender: 'ifArchiveExtendedApply' },
          },
          {
            title: '归档资料状态',
            dataIndex: 'archiveDataStatus',
            scopedSlots: { customRender: 'archiveDataStatus' },
          },
        ]
        const convertKeys = [ 'ifOver', 'ifArchiveExtendedApply']
        const list = (res.data || []).map((item) => {
          convertKeys.forEach((key) => {
            item[key] = item[key] ? '是' : '否'
          })
          // 当资料审核状态为通过时，为已归档，其余情况为未归档
          item.ifCheckArchive = item.archiveDataStatus == 2 ? '是' : '否'
          item.archiveDataStatus = STATUS_MAP_Number[item.archiveDataStatus]?.label
          return item
        })
        this.exportFileName = `${branchOrgName}${archiveYear}年度归档台账`
        this.$refs.exportBtn.generateExcel(list, { columns })
      } catch (e) {
        console.error(e)
      }
      data.loading = false
    },
    /**
     * 处理数据的相关操作（增删改查）
     */
    handleUpdate(data, pageType) {
      const commonHandler = () => {
        this.$router.push({
          path: '/conference/archiveManage/archiveStandingBook/update',
          query: {
            id: data.id || '',
            pageType: pageType,
            branchOrgId: data.branchOrgId || '',
            archiveYear: data.archiveYear || '',
            branchOrgId: data.branchOrgId || '',
            branchOrgName: data.branchOrgName || '',
          },
        })
      }
      const actionMap = {
        look: commonHandler,
        export: () => {
          this.handleExport(data)
        },
      }
      actionMap[pageType] && actionMap[pageType]()
    },
    handleSearch(val = {}) {
      this.queryParam = {
        ...val,
      }
      this.refreshTable()
    },
    refreshTable() {
      this.$refs.table.refresh(true)
    },
    async loadData(parameter) {
      const requestParameters = Object.assign({}, { ...parameter }, { query: this.queryParam })
      return getArchiveStandingBook(requestParameters)
    },
  },
}
</script>

<style>
</style>